package com.mmo.cq.game.script.server;

import com.mmo.cq.game.server.bean.server.ServerConfig;
import com.mmo.cq.game.server.bean.server.Server_Seed;
import com.mmo.cq.game.server.data.DataCenter;
import lombok.extern.slf4j.Slf4j;
import org.wxd.batis.mongodb.MongoDataHelper;
import org.wxd.ioc.IocInjector;
import org.wxd.ioc.i.IShutdown;
import org.wxd.ioc.i.IStart;
import org.wxd.timer.ann.Scheduled;

import javax.annotation.Resource;

/**
 * 全服数据模型
 *
 * @author: Troy.Chen(無心道, 15388152619)
 * @version: 2023-02-02 16:24
 **/
@Slf4j
@Resource
public class ServerDataModule implements IStart, IShutdown {

    @Resource ServerConfig serverConfig;
    @Resource MongoDataHelper mongoDataHelper;
    @Resource DataCenter dataCenter;

    @Override public void start(IocInjector iocInjector) throws Exception {
        Server_Seed temp_serverSeed = mongoDataHelper.queryEntity(Server_Seed.class, serverConfig.getSid());
        if (temp_serverSeed == null) {
            temp_serverSeed = new Server_Seed();
            temp_serverSeed.setSid(serverConfig.getSid());
        }
        dataCenter.setServerSeed(temp_serverSeed);
        mongoDataHelper.replace(temp_serverSeed);
    }

    public long newUid(Server_Seed.Type type) {
        int sid = serverConfig.getSid();
        return dataCenter.getServerSeed().newUid(type, sid);
    }

    @Override public void shutdown() throws Exception {
        mongoDataHelper.replace(dataCenter.getServerSeed());
    }

    @Scheduled("0 * *")
    public void t1() {
        log.info("定时器:{}", this.hashCode());
    }


}
